//
//  IdeaViewController.m
//  ShangAPP
//
//  Created by 徐番茄丶 on 16/1/23.
//  Copyright © 2016年 wangdao. All rights reserved.
//

#import "IdeaViewController.h"
#import "MyTools.h"
@interface IdeaViewController ()<UITextFieldDelegate,UITextViewDelegate>{
    NSString *_textstr;
}
@property (weak, nonatomic) IBOutlet UIButton    *sendbrn;
@property (weak, nonatomic) IBOutlet UITextView  *myTextview;
@property (weak, nonatomic) IBOutlet UITextField *myTextfield;

@end

@implementation IdeaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"意见反馈";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"arrow"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    [self.navigationItem.leftBarButtonItem setTintColor:UIColorFromRGB(0x444444)];
    
    [self viewCreate];
}
-(void)viewCreate{
    UIButton *backbtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    [backbtn addTarget:self action:@selector(textback) forControlEvents:UIControlEventTouchUpInside];
    [self.view insertSubview:backbtn atIndex:0];
    
    _myTextview.layer.cornerRadius = 5;
    _myTextview.layer.borderWidth = 1;
    _myTextview.layer.borderColor = [UIColorFromRGB(0xcecece) CGColor];
    _myTextview.delegate = self;
    
    _myTextfield.layer.cornerRadius = 5;
    _myTextfield.layer.borderWidth = 1;
    _myTextfield.layer.borderColor = [UIColorFromRGB(0xcecece) CGColor];
    _myTextfield.delegate = self;
    
    _sendbrn.layer.cornerRadius = 5;
    [_sendbrn addTarget:self action:@selector(sendBtn) forControlEvents:UIControlEventTouchUpInside];
    _textstr = [NSString stringWithFormat:@"给我们一些建议，让我们做得更好（500字以内）"];
}

- (void)sendBtn
{
    NSString *url = @"http://www.xsy777.com/app/merchant/feedback.html";
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
    NSDictionary *dic1 = @{
                           @"user_token":[user objectForKey:@"token"],
                           @"phone"     :_myTextfield.text,
                           @"contents"  :_myTextview.text
                           };
    
    AFHTTPRequestOperationManager *manager = [MyTools initAFHttpManager];
    
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    [manager POST:url parameters:dic1 success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        
        if ([[responseObject objectForKey:@"status"] intValue] == 1) {
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
            _myTextview.text =nil;_myTextfield.text =nil;
            
        }else{
            [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
            [self.view makeToast:[responseObject objectForKey:@"info"] duration:1.0 position:CSToastPositionCenter];
        }
        
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        
        if ([MyTools connectedToNetwork] == 0) {
            [self.view makeToast:@"请检查是否联网" duration:1.0 position:CSToastPositionCenter];
        }else{
            [self.view makeToast:@"服务器错误，再试一次" duration:1.0 position:CSToastPositionCenter];
        }
        
    }];
}

#pragma mark textfield代理
//开始编辑输入框的时候，软键盘出现，执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGRect frame = textField.frame;
    int offset = frame.origin.y + 50 - (self.view.frame.size.height - 216.0);//键盘高度216
    
    [UIView animateWithDuration:0.5 animations:^{
        //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
        if(offset > 0)
            self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
    }];
}

//当用户按下return键或者按回车键，keyboard消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

//输入框编辑完成以后，将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    [UIView animateWithDuration:0.5 animations:^{
        self.view.frame =CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height);
        
    }];
}





#pragma mark textview代理

//开始编辑输入框的时候，软键盘出现，执行此事件
- (void)textViewDidBeginEditing:(UITextView *)textView{
    if ([textView.text isEqualToString:_textstr]) {
        textView.text = @"";
    }
}

//输入框编辑完成以后，将视图恢复到原始状态
- (void)textViewDidEndEditing:(UITextView *)textView{
    if (textView.text.length < 1) {
        textView.text = _textstr;
    }
    
}
//当用户按下return键或者按回车键，keyboard消失
-(BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
    if ([text isEqualToString:@"\n"]) {
        
        [textView resignFirstResponder];
        
        return NO;
        
    }
    
    return YES;
    
}


-(void)textback{
    [_myTextfield resignFirstResponder];
    [_myTextview resignFirstResponder];
}

-(void)back{
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.translucent = YES;
    self.tabBarController.tabBar.hidden = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    self.tabBarController.tabBar.translucent = NO;
    self.tabBarController.tabBar.hidden = NO;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
